﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using GameEngine.SceneGraph;


namespace Redland2
{
    public class Bullet : Entity
    {
        #region members
        private const float BULLET_BASE_SPEED = 10.0f;
        private int _damage; 
        #endregion members

        #region properties
        public int Damage
        {
            get { return _damage; }
        }
        #endregion properties

        #region constructors
        public Bullet(GameScene inputScene, int inputIndex, Player inputOwner, Vector2 position, Vector2 direction, int inputDamage ): base(inputScene, inputIndex, GameScene.Current.Content.Load<Texture2D>("bullet"), position)
        {
            this.type = "bullet";
            SetupBullet();
            direction.Normalize();
            this.Velocity = direction * BULLET_BASE_SPEED;
            this.Owner = inputOwner;
            this._damage = inputDamage;
        }

        private void SetupBullet()
        { 
            this.SetOffset(GameEngine.Drawables.Anchor.Center);
        }
        #endregion constructors

        #region methods

        public void RotateTodirection()
        {
            Vector2 direction = this.Velocity;
            direction.Normalize();
            this.Angle = (float)Math.Atan2(direction.Y, direction.X) + MathHelper.PiOver2;
        }

        public override void Update(GameTime gameTime)
        {
            RotateTodirection();
            base.Update(gameTime);
        }

        public override void HandleCollision()
        {
            _collisions.Remove(Owner);
            foreach (Entity entity in _collisions)
            {
                if (entity.type == "bullet" || entity.type == "hole" || entity.type == "lava" || entity.type == "powerup")
                    continue;
                else
                    this.Kill();
            }
            
        }

        #endregion methods 
    }
}
